{extend name="common/application" /}
{block name="title"}<title id="title">店铺中心-发布商品</title>{/block}
{block name="css"}
<link rel="stylesheet" type="text/css" href="_CSS_/reset.css">
<link rel="stylesheet" type="text/css" href="_CSS_/common.css">
<link rel="stylesheet" type="text/css" href="_CSS_/help.css"/>
<link rel="stylesheet" type="text/css" href="_CSS_/store_center.css"/>
<!--[if lt IE 9]>
<script src="http://www.ijquery.cn/js/html5shiv.js"></script>
<![endif]-->
{/block}


{block name="content"}
<!-- 主体内容 -->
<div class="main">
    <div class="mainBody normalBox clearfix">
        <!--左菜单-->

        {include file="/common/left" /}
        <!--右内容-->
        <div class="menuContent fr">
            <div class="menuConHeader">
                <!--面包屑导航-->
                <div class="breadcrumb">
                    <p>
                        <a href="javascript:;">发布商品</a> &gt;
                        <a class="curPage" href="{:url('index/store/publish')}">发布商品</a>
                    </p>
                </div>
            </div>
            <div class="menuConBody">
                <div class="courseMenu">
                    <div class="courseSelect">
                        <ul>
                            <li class="selected">
                                <a class="selectType" href="javascript:;">交易类商品</a>
                            </li>
                            <li>
                                <a class="selectType" href="javascript:;">服务类商品</a>
                            </li>
                        </ul>
                    </div>
                    <div class="conBody">
                        <!-- 交易商品发布 -->
                        <div class="publist-box">
                            <form id="form-admin-add">
                                <div class="row">
                                    <label class="pub-left">专利号:</label>
                                    <div class="pub-right">
                                        <input type="text" class="name" id="order" name="order" placeholder="请输入专利号">
                                    </div>
                                </div>
                                <div class="row">
                                    <label class="pub-left">商品名称:</label>
                                    <div class="pub-right">
                                        <input type="text" class="name" id="name" disabled>
                                    </div>
                                </div>
                                <div class="row">
                                    <label class="pub-left">交易分类:</label>
                                    <div class="pub-right">
                                        <select class="select name" name="p_id">
                                            <option value="0">请选择</option>
                                            <!--<option value="1">版权</option>-->
                                            <!--<option value="7">商标</option>-->
                                            <!--<option value="11">专有技术</option>-->
                                            <option value="14">专利</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="row">
                                    <label class="pub-left">行业分类:</label>
                                    <div class="pub-right">
                                        <select class="select name" name="category_id">
                                        </select>
                                    </div>
                                </div>
                                <div class="row">
                                    <label class="pub-left">交易方式:</label>
                                    <div class="pub-right">
                                        <select class="select name" name="c_id">
                                            <option value="0">请选择</option>
                                            <option value="1">转让</option>
                                            <option value="2">独占许可</option>
                                            <option value="3">排他许可</option>
                                            <option value="4">分许可</option>
                                            <option value="10">普通许可</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="row">
                                    <label class="pub-left">发布期限:</label>
                                    <div class="pub-right">
                                        <select class="select name" name="update">
                                            <option value="0">请选择</option>
                                            <option value="1">一个月</option>
                                            <option value="2">三个月</option>
                                            <option value="3">半年</option>
                                            <option value="4">一年</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="row">
                                    <label class="pub-left">建议售价:</label>
                                    <div class="pub-right">
                                        <input type="text" class="name" onkeyup="value=value.replace(/[^\d]/g,'')" id="store" placeholder="请输入商品价格">
                                      <!--  <span class="tip-span">注：售价不得低于￥1000，高于￥50000</span> -->
                                    </div>
                                </div>
                                <div class="row">
                                    <label class="pub-left">授权量:</label>
                                    <div class="pub-right">
                                        <input type="text" class="name" onkeyup="value=value.replace(/[^\d]/g,'')" name="value" placeholder="请输入授权量">
                                    </div>
                                </div>
                                <!--<div class="row">-->
                                <!--<label class="pub-left">授权书:</label>-->
                                <!--<div class="pub-right">-->
                                <!--<div class="upload-img">-->
                                <!--<div class="upload-btn">-->
                                <!--<img src="/static/web/images/upload_authorization.jpg">-->
                                <!--<input type="file" name="pic" id="file0"-->
                                <!--onchange="previewImage(this,0)"-->
                                <!--accept="image/gif, image/jpeg, image/png">-->
                                <!--</div>-->
                                <!--</div>-->
                                <!--<p class="tip-p">注：仅支持JPG、PNG、JPEG格式，.图片大小不能超过3M</p>-->
                                <!--</div>-->
                                <!--</div>-->
                                <div class="row">
                                    <label class="pub-left">授权书:</label>
                                    <div class="pub-right">
                                        <div class="upload-img">
                                            <div class="upload-btn">
                                                <img src="/static/web/images/upload_authorization.jpg">
                                                <input type="file" name="pic" id="file0" onchange="previewImage(this,0)"
                                                       accept="image/gif, image/jpeg, image/png">
                                            </div>
                                            <div class="img-info hide">
                                                <img src=""/>
                                                <div class="file-panel">
                                                    <div class="del-cancel" id='delete0'>
                                                        <a class="btn table-btn" title="删除"
                                                           href="javascript:;">&times;</a>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <p class="tip-p">注：仅支持JPG、PNG、JPEG格式，.图片大小不能超过3M</p>
                                    </div>
                                </div>
                                <input type="hidden" id="images" name="images">
                                <div class="row">
                                    <label class="pub-left">商品图片:</label>
                                    <div class="pub-right">
                                        <div class="upload-img">
                                            <div class="upload-btn">
                                                <img src="/static/web/images/upload_goods.jpg">
                                                <input type="file" name="image" id="file1"
                                                       onchange="previewImage(this,1)"
                                                       accept="image/gif, image/jpeg, image/png">
                                            </div>
                                            <div class="img-info hide">
                                                <img src=""/>
                                                <div class="file-panel">
                                                    <div class="del-cancel" id="delete1">
                                                        <a class="btn table-btn" title="删除"
                                                           href="javascript:;">&times;</a>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="img-list">

                                        </div>
                                        <p class="tip-p">
                                            注：仅支持JPG、PNG、JPEG格式，图片大小不能超过3M，您可以自己上传商品图片，也可以选择旁边的图片，一个商品类目下只能上传或选择一张图片。</p>
                                    </div>
                                </div>
                                <!--<div class="row">-->
                                <!--<label class="pub-left">商品图片:</label>-->
                                <!--<div class="pub-right">-->
                                <!--<div class="upload-img">-->
                                <!--<div class="upload-btn">-->
                                <!--<img src="/static/web/images/upload_authorization.jpg">-->
                                <!--<input type="file" name="image" id="file1"-->
                                <!--onchange="previewImage(this,1)"-->
                                <!--accept="image/gif, image/jpeg, image/png">-->
                                <!--</div>-->
                                <!--</div>-->
                                <!--<div class="img-list">-->
                                <!--<img class="border-img" src="images/4.png">-->
                                <!--<img src="images/4.png">-->
                                <!--<img src="images/4.png">-->
                                <!--<img src="images/4.png">-->
                                <!--<img src="images/4.png">-->
                                <!--<img src="images/4.png">-->
                                <!--<img src="images/4.png">-->
                                <!--<img src="images/4.png">-->
                                <!--<img src="images/4.png">-->
                                <!--<img src="images/4.png">-->
                                <!--<img src="images/4.png">-->
                                <!--<img src="images/4.png">-->
                                <!--<img src="images/4.png">-->
                                <!--<img src="images/4.png">-->
                                <!--<img src="images/4.png">-->
                                <!--<img src="images/4.png">-->
                                <!--<img src="images/4.png">-->
                                <!--<img src="images/4.png">-->
                                <!--</div>-->
                                <!---->
                                <!--<p class="tip-p">-->
                                <!--注：仅支持JPG、PNG、JPEG格式，图片大小不能超过3M，您可以自己上传商品图片，一个商品类目下只能上传或选择一张图片。</p>-->
                                <!--</div>-->
                                <!--</div>-->
                                <div class="row">
                                    <label class="pub-left">商品描述:</label>
                                    <div class="pub-right">
                                        <textarea class="name" name="content"></textarea>
                                    </div>
                                </div>
                                <div class="row">
                                    <label class="pub-left"></label>
                                    <div class="pub-right">
                                        <a class="publish-btn" id="trading" href="javascript:;">确认发布</a>
                                    </div>
                                </div>
                            </form>
                        </div>
                        <!-- 服务商品发布 -->
                        <div class="publist-box hide">
                            <form>
                                <div class="row">
                                    <label class="pub-left">商品名称:</label>
                                    <div class="pub-right">
                                        <input type="text" class="service" name="goods_name" placeholder="请输入商品名称">
                                    </div>
                                </div>
                                <div class="row">
                                    <label class="pub-left">服务分类:</label>
                                    <div class="pub-right">
                                        <select class="select service" name="classify">
                                            <option value="0">请选择</option>
                                            <option value="1">专利评议</option>
                                            <option value="2">专利维权</option>
                                            <option value="3">专利托管</option>
                                            <option value="4">专利代理</option>
                                            <option value="5">政策应用</option>
                                            <option value="6">设计开发</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="row king">
                                    <label class="pub-left">服务内容:</label>
                                    <div class="pub-right">
                                        <select class="select service" name="type">
                                            <option value="0">请选择</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="row">
                                    <label class="pub-left">商品价格:</label>
                                    <div class="pub-right">
                                        <input type="text"  class="service" onkeyup="value=value.replace(/[^\d]/g,'')" id="price" placeholder="请输入商品价格">
                                        <!-- <span class="tip-span">注：售价不得低于￥1000，高于￥50000</span> -->
                                    </div>
                                </div>
                                <div class="row">
                                    <label class="pub-left">商品图片:</label>
                                    <div class="pub-right">
                                        <div class="upload-img">
                                            <div class="upload-btn">
                                                <img src="/static/web/images/upload_goods.jpg">
                                                <input type="file" name="pic" id="file2"
                                                       onchange="previewImage(this,2)"
                                                       accept="image/gif, image/jpeg, image/png">
                                            </div>
                                            <div class="img-info hide">
                                                <img src=""/>
                                                <div class="file-panel">
                                                    <div class="del-cancel" id="delete2">
                                                        <a class="btn table-btn" title="删除"
                                                           href="javascript:;">&times;</a>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <p class="tip-p">注：仅支持JPG、PNG、JPEG格式，.图片大小不能超过3M</p>
                                    </div>
                                </div>
                                <div class="row">
                                    <label class="pub-left">商品描述:</label>
                                    <div class="pub-right">
                                        <textarea class="service" name="content"></textarea>
                                    </div>
                                </div>
                                <div class="row">
                                    <label class="pub-left"></label>
                                    <div class="pub-right">
                                        <a class="publish-btn" id="service" href="javascript:;">确认发布</a>
                                    </div>
                                </div>
                            </form>
                        </div>


                    </div>
                </div>
            </div>

        </div>
    </div>
</div>
{/block}
{block name="js"}
<!-- 脚本引入 -->
<script src="_P_/layer/layer.js"></script>
<script>
    $(function () {
        $(".row").eq(3).hide();
        $(".king").hide();
        var userinfo = $.cookie('userinfo');
        var user_login = $.base64.decode(userinfo).split(",");
        if (userinfo != null) {
            $.ajax({
                type: "get",
                url: "{:url('api/shop/publish_show')}",
                data: {'uid': user_login[0]},
                dataType: 'jsonp',
                success: function (msg) {
                    if (msg.code == 200) {
                        if (msg.data == 1) {
                            $(".selectType").eq(1).hide();
                        }
                    } else {
                        layer.msg(msg.message, {icon: 5, time: 5000});
                    }
                }
            })
        } else {
            window.location.href = "{:url('index/login/index')}";
        }
        $.ajax({
            type: "get",
            url: "{:url('api/shop/cation')}",
            data: {'uid': user_login[0]},
            dataType: 'jsonp',
            success: function (msg) {
                if (msg.code == 200) {
                } else if (msg.code == 1000) {
                    layer.msg(msg.message, {icon: 5, time: 2000}, function () {
                        window.location.href = "{:url('index/store/store_open1')}";
                    });
                } else if (msg.code == 1001) {
                    layer.msg(msg.message, {icon: 5, time: 2000}, function () {
                        window.location.href = "{:url('index/store/store_open6')}";
                    });
                }
            }
        })
        getfile();
    })
    $(document).ready(function () {
        $('.li1').addClass('showChild');
        $('.li10').addClass('activeMenu');
    });
	
	$("#order").change(function () {
        var order = $("#order").val();
        $.ajax({
            type: "get",
            url: "{:url('api/shop/order_name')}",
            data: {order: order},
            dataType: 'jsonp',
            success: function (msg) {
                if (msg.code == 200) {
                    $("#name").val(msg.data);
                }
                if (msg.code == 1001) {
                    $("#name").val('');
                    layer.msg(msg.message, {icon: 5, time: 2000});
                }
                if (msg.code == 1000) {
                    $("#name").val('');
                    layer.msg(msg.message, {icon: 5, time: 2000});
                }
            }
        })
    })

	
    $(document).ready(function () {
        /*交易商品服务商品切换*/
        $(".courseSelect li").on("click", function () {
            var num = $(".courseSelect li").index(this);
            $(".courseSelect li").removeClass("selected");
            $(this).addClass("selected");
            $(".publist-box").hide();
            $(".publist-box").eq(num).show();
        });

        /*图片选择*/
        // $(".img-list img").on("click", function () {
        //     var num = $(".img-list img").index(this);
        //     $(".img-list img").removeClass("border-img");
        //     $(this).addClass("border-img");
        // });
    });

    function getfile() {
        $.ajax({
            type: "get",
            url: "{:url('api/shop/material')}",
            dataType: 'jsonp',
            success: function (msg) {
                if (msg.code == 200) {
                    if (msg.data.length > 0) {
                        var html = '';
                        for (var i = 0; i < msg.data.length; i++) {
                            html += '<img src="/Images/tradingMarket/' + msg.data[i].image + '">';
                        }
                        $(".img-list").html(html);
                        /*图片选择*/
                        $(".img-list img").on("click", function () {
                            var num = $(".img-list img").index(this);
                            $(".img-list img").removeClass("border-img");
                            $(this).addClass("border-img");
                            var value = $(this).attr('src');
                            var arr = value.split('/Images/tradingMarket/');
                            $("#images").val(arr[1]);


                            // $(this).parents(".row").find(".upload-img").prepend('<div class="img-info"><img src="'+value+'"/><div class="file-panel"><div class="del-cancel"><a class="btn table-btn" title="删除" href="javascript:;">&times;</a></div></div></div>');
                            $(this).parents(".row").find('.img-info').show();
                            $(this).parents(".row").find('.img-info').find('img').attr('src', value);
                            $(this).parents(".row").find(".upload-btn").hide();
                            $(this).parents(".row").find(".upload-img").children('p').hide();
                            $('.img-info').hover(function () {
                                $(this).find(".file-panel").show();
                            }, function () {
                                $(this).find(".file-panel").hide();

                            });

                            $("#delete1 a").on("click", function () {
                                $('#file' + 1).val("");
                                $(this).parents('.upload-img').find(".upload-btn").show();
                                $(this).parents('.upload-img').find(".upload-img p").show();
                                $(this).parents(".img-info").hide();
                                $("#images").val('');
                            });
                        });


                    }
                } else {
                    layer.msg(msg.message, {icon: 5, time: 5000});
                }
            }
        })
    }

    $(".name").eq(2).on('click', function () {
        var id = $(this).val();
        if (id == 0) {
            $(".row").eq(3).hide();
        } else {
            $(".row").eq(3).show();
        }
        $.ajax({
            type: "get",
            url: "{:url('api/shop/category')}",
            data: {id: id},
            dataType: 'jsonp',
            success: function (msg) {
                if (msg.code == 200) {
                    if (msg.data.length > 0) {
                        var html = '<option value="0">请选择</option>';
                        for (var i = 0; i < msg.data.length; i++) {
                            html += '<option value="' + msg.data[i].id + '">' + msg.data[i].market_name + '</option>';
                        }
                        $(".name").eq(3).html(html);
                    }
                } else {
                    layer.msg(msg.message, {icon: 5, time: 5000});
                }
            }
        })
    });

    $(".service").eq(1).on('click', function () {
        var id = $(this).val();
        if (id == 0) {
            $(".king").hide();
        } else {
            $(".king").show();
        }
        $.ajax({
            type: "get",
            url: "{:url('api/shop/category_service')}",
            data: {id: id},
            dataType: 'jsonp',
            success: function (msg) {
                if (msg.code == 200) {
                    if (msg.data.length > 0) {
                        var html = '<option value="0">请选择</option>';
                        for (var i = 0; i < msg.data.length; i++) {
                            html += '<option value="' + msg.data[i].id + '">' + msg.data[i].t_name + '</option>';
                        }
                        $(".service").eq(2).html(html);
                    }
                } else {
                    layer.msg(msg.message, {icon: 5, time: 5000});
                }
            }
        })
    });

    $("#trading").on('click', function () {
        var userinfo = $.cookie('userinfo');
        if (userinfo != null) {
			var store = $('#store').val();
			if(store == 0){
				layer.alert('价格不能小于1元');
				return;
			}
			var name = $("#name").val();
            var images = $("#images").val();
            var user_login = $.base64.decode(userinfo).split(",");
            var formData = new FormData($('form')[0]);
            formData.append('user', user_login[0]);
            formData.append('show', 1);
            formData.append('images', images);
            formData.append('store', store);
			formData.append('username', name);
            $.ajax({
                type: "post",
                url: "{:url('api/shop/publish')}",
                data: formData,
                dataType: 'json',
				beforeSend: function () {
                var index = layer.load(1, {
                    shade: [0.1, '#333333'] //0.1透明度的白色背景
                });
				},
                success: function (msg) {
                    layer.closeAll();
                    if (msg.code == 200) {
                        layer.msg(msg.message, {icon: 1, time: 3000}, function () {
                            window.location.href = '/index.php/index/store/noshelves';
                        });
                    } else {
                        layer.msg(msg.message, {icon: 5, time: 5000});
                    }
                },
                contentType: false, //必须false才会自动加上正确的Content-Type
                processData: false //必须false才会避开jQuery对 formdata 的默认处理
            })
        } else {
            window.location.href = "{:url('index/login/index')}";
        }
    });

    $("#service").on('click', function () {
        var userinfo = $.cookie('userinfo');
        if (userinfo != null) {
			var price = $("#price").val();
			if(price == 0){
				layer.alert('价格不能小于1元');
				return;
			}
            var user_login = $.base64.decode(userinfo).split(",");
            var formData = new FormData($('form')[1]);
            formData.append('user', user_login[0]);
            formData.append('show', 0);
            formData.append('price', price);
            $.ajax({
                type: "post",
                url: "{:url('api/shop/publish')}",
                data: formData,
                dataType: 'json',
				beforeSend: function () {
                var index = layer.load(1, {
                    shade: [0.1, '#333333'] //0.1透明度的白色背景
                });
				},
                success: function (msg) {
                    if (msg.code == 200) {
						layer.msg(msg.message, {icon: 1, time: 3000}, function () {
                            window.location.href = '/index.php/index/store/noshelves';
                        });
                    } else {
                        layer.msg(msg.message, {icon: 5, time: 5000});
                    }
                },
                contentType: false, //必须false才会自动加上正确的Content-Type
                processData: false //必须false才会避开jQuery对 formdata 的默认处理
            })
        } else {
            window.location.href = "{:url('index/login/index')}";
        }
    });


    /* 图片上传*/
    function previewImage(obj, num) {
        var filepath = $(obj).val();
        fileObj = document.getElementById('file' + num);
        value = window.URL.createObjectURL(fileObj.files[0]);
        file_size = obj.files[0].size;
        size = file_size / 1024;
        if (size > 1024 * 5) {
            $('#file' + num).val("");
            layer.alert('上传图片不得大于5MB', {
                closeBtn: 0
            });
        } else {
            /*$(obj).parents(".upload-img").prepend('<div class="img-info"><img src="'+value+'"/><div class="file-panel"><div class="del-cancel"><a class="btn table-btn" title="删除" href="javascript:;">&times;</a></div></div></div>');*/
            $(obj).parents(".upload-img").find('.img-info').show();
            $(obj).parents(".upload-img").find('.img-info').find('img').attr('src', value);
            $(obj).parent(".upload-btn").hide();
            $(obj).parents(".upload-img").children('p').hide();

            $('.img-info').hover(function () {
                $(this).find(".file-panel").show();
            }, function () {
                $(this).find(".file-panel").hide();

            });

            $("#delete0 a").on("click", function () {
                $('#file' + 0).val("");
                $(this).parents('.upload-img').find(".upload-btn").show();
                $(this).parents('.upload-img').find(".upload-img p").show();
                $(this).parents(".img-info").hide();

            });
            $("#delete1 a").on("click", function () {
                $('#file' + 1).val("");
                $(this).parents('.upload-img').find(".upload-btn").show();
                $(this).parents('.upload-img').find(".upload-img p").show();
                $(this).parents(".img-info").hide();
                $("#images").val('');
            });
            $("#delete2 a").on("click", function () {
                $('#file' + 2).val("");
                $(this).parents('.upload-img').find(".upload-btn").show();
                $(this).parents('.upload-img').find(".upload-img p").show();
                $(this).parents(".img-info").hide();
            });
        }
    }

</script>
{/block}